package com.hsd.znsh.controller.root;

import com.hsd.znsh.entity.Function;
import com.hsd.znsh.entity.ProductType;
import com.hsd.znsh.pojo.output.ResultVO;
import com.hsd.znsh.pojo.output.TreeNode;
import com.hsd.znsh.service.ProductTypeService;
import com.hsd.znsh.utils.ResultVOUtil;
import com.hsd.znsh.utils.TreeUtil;
import com.hsd.znsh.utils.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/*
 * @auther: wangjiacai
 *
 * @create: 2018-05-07 09:12:16
 */
@RequestMapping("/root/product")
@RestController
public class RootProductController {

    @Autowired
    private ProductTypeService productTypeService;

    @GetMapping("/getProductTypeList")
    public List<TreeNode> getProductTypeList(){
        Map<Integer, TreeNode> nodelist = productTypeService.getProductTypeList();
        return TreeUtil.getNodeList(nodelist);
    }

    @GetMapping("/getProductTypeInfo/{id}")
    public ResultVO getProductTypeInfo(@PathVariable("id") Integer id){
        ProductType productType=productTypeService.getProductTypeInfo(id);
        return ResultVOUtil.success(productType);
    }

    @PostMapping("/deletedProductType/{id}")
    public ResultVO deletedProductType(@PathVariable("id") Integer id){
        productTypeService.deletedProductType(id);
        return ResultVOUtil.success("删除成功");
    }

    @PostMapping("/editProductType")
    public ResultVO updateProductType(@Valid ProductType productType, BindingResult bindingResult){
        ValidatorUtil.valid(bindingResult);
        if(productType.getId()==null){
            productTypeService.addProductType(productType);
            return ResultVOUtil.success("添加成功");
        }else{
            productTypeService.updateProductType(productType);
            return ResultVOUtil.success("修改成功");
        }
    }

}
